package com.whp.openapi.web.controller;

import com.whp.openapi.common.Const;
import com.whp.openapi.common.ResponseResult;
import com.whp.openapi.common.ResultTable;
import com.whp.openapi.entity.AdminUser;
import com.whp.openapi.entity.Menu;
import com.whp.openapi.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author whp
 * @date 2021/11/24
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    
    @Autowired
    private MenuService menuService;

    /**
     * 展示左侧菜单栏
     * @param session
     * @return
     */
    @RequestMapping("/getMenu")
    public ResponseResult getMenu(HttpSession session) {
        AdminUser user = (AdminUser) session.getAttribute(Const.IS_LOGIN);
        if (user == null) {
            return ResponseResult.getFail("请先登录！");
        }
        //获取用户拥有的菜单（用户权限）
        List<Menu> menus = menuService.getMenuByUid(user.getId());
        return ResponseResult.getSuccess(menus);
    }

    /**
     * 菜单管理（展示所有菜单）
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/list")
    public ResultTable getList(Integer page, Integer limit){
        return menuService.getList(page, limit);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequestMapping("/del")
    public ResponseResult delMenus(Integer[] ids) {
        return menuService.delMenus(ids);
    }

    /**
     * 添加
     * @param menu
     * @return
     */
    @RequestMapping("/add")
    public ResponseResult addMenu(Menu menu) {
        return menuService.addMenu(menu);
    }

    /**
     * 编辑
     * @param menu
     * @return
     */
    @RequestMapping("/update")
    public ResponseResult updateMenu(Menu menu) {
        return menuService.updateMenu(menu);
    }
    

    /**
     * 显示弹框中权限的相应信息（编辑）
     * @param id
     * @return
     */
    @RequestMapping("/info")
    public Menu getInfo(Integer id){
        return menuService.getMenuById(id);
    }
}
